package com.ifonly.common;

import java.io.Serializable;

/**
 * AjaxMessage
 *
 *  用于返回统一格式的数据信息
 *
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-10 21:56
 * @since JDK 1.6
 */
public class ApiMessage<E> implements Serializable {
    private static final long serialVersionUID = -5027138000794471389L;

    private int status;
    private String message;
    private Throwable throwable;
    private E data;

    private ApiMessage(int status, String message) {
        this(status, message, null);
    }

    private ApiMessage(int status, String message, E data) {
        this(status, message, data, null);
    }

    private ApiMessage(int status, String message, E data, Throwable throwable) {
        this.status = status;
        this.message = message;
        this.data = data;
        this.throwable = throwable;
    }

    public static <E> ApiMessage<E> success() {
        return new ApiMessage<E>(Status.SUCCESS.status, Status.SUCCESS.message);
    }

    public static <E> ApiMessage<E> success(String message) {
        return new ApiMessage<E>(Status.SUCCESS.status, message);
    }

    public static <E> ApiMessage<E> success(E data) {
        return new ApiMessage<E>(Status.SUCCESS.status, Status.SUCCESS.message, data);
    }

    public static <E> ApiMessage<E> success(String message, E data) {
        return new ApiMessage<E>(Status.SUCCESS.status, message, data);
    }

    public static <E> ApiMessage<E> failed() {
        return new ApiMessage<E>(Status.FAILED.status, Status.FAILED.message);
    }

    public static <E> ApiMessage<E> failed(String message) {
        return new ApiMessage<E>(Status.FAILED.status, message);
    }

    public static <E> ApiMessage<E> failed(E data) {
        return new ApiMessage<E>(Status.FAILED.status, Status.FAILED.message, data);
    }

    public static <E> ApiMessage<E> failed(String message, E data) {
        return new ApiMessage<E>(Status.FAILED.status, message, data);
    }

    public static <E> ApiMessage<E> failed(Throwable throwable) {
        return new ApiMessage<E>(Status.FAILED.status, Status.FAILED.message, null, throwable);
    }

    public static <E> ApiMessage<E> failed(String message, Throwable throwable) {
        return new ApiMessage<E>(Status.FAILED.status, message, null, throwable);
    }

    public static <E> ApiMessage<E> failed(String message, E data, Throwable throwable) {
        return new ApiMessage<E>(Status.FAILED.status, message, data, throwable);
    }

    public static <E> ApiMessage<E> create(int status, String message, E data, Throwable throwable) {
        return new ApiMessage<E>(status, message, data, throwable);
    }


    enum Status {
        SUCCESS(1, "操作成功"),
        FAILED(0, "操作失败");

        public int status;
        public String message;
        Status(int status, String message) {
            this.status = status;
            this.message = message;
        }
    }

    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public E getData() {
        return data;
    }
}
